package _interview150;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 228. 汇总区间
 */
public class No228 {
    public List<String> summaryRanges(int[] nums) {
        int n = nums.length;
        if (n == 1) return Collections.singletonList(nums[0] + "");

        List<String> ans = new ArrayList<>();
        int len = 1;
        for (int i = 1; i <= n; i++) {
            while (i < n && nums[i] == nums[i - 1] + 1) {
                len++;
                i++;
            }
            int start = i - len;
            ans.add(len == 1 ? nums[start] + "" : nums[start] + "->" + nums[i - 1]);
            len = 1;
        }
        return ans;
    }
}
